home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 12 / BBS in a box XII-1.iso / Files / Unstuffers / Other Compression / MacGzip 0.21-src-c.sit / macgzip_021-src / zip.c < prev   
Encoding:
C/C++ Source or Header  |  1993-11-08  |  3.3 KB  |  132 lines  |  [TEXT/KAHL]

  1. /* zip.c -- compress files to the gzip or pkzip format
  2.  * Copyright (C) 1992-1993 Jean-loup Gailly
  3.  * This is free software; you can redistribute it and/or modify it under the
  4.  * terms of the GNU General Public License, see the file COPYING.
  5.  */
  6.  
  7. /*
  8.  * Modified:1993 by SPDsoft for MacGzip
  9.  *
  10.  */
  11.  
  12. #ifdef RCSID
  13. static char rcsid[] = "$Id: zip.c,v 0.17 1993/06/10 13:29:25 jloup Exp $";
  14. #endif
  15.  
  16. #include <ctype.h>
  17. #include <sys/types.h>
  18.  
  19. #include "tailor.h"
  20. #include "gzip.h"
  21. #include "crypt.h"
  22.  
  23. #ifdef HAVE_UNISTD_H
  24. #  include <unistd.h>
  25. #endif
  26. #ifndef NO_FCNTL_H
  27. #  include <fcntl.h>
  28. #endif
  29. #include "SPDProg.h"
  30. #include <signal.h>
  31. extern long tell(int fn);
  32.  
  33.  
  34. local ulg crc;       /* crc on uncompressed file data */
  35. long header_bytes;   /* number of bytes in gzip header */
  36.  
  37. /* ===========================================================================
  38.  * Deflate in to out.
  39.  * IN assertions: the input and output buffers are cleared.
  40.  *   The variables time_stamp and save_orig_name are initialized.
  41.  */
  42. int zip(in, out)
  43.     int in, out;            /* input and output file descriptors */
  44. {
  45.     uch  flags = 0;         /* general purpose bit flags */
  46.     ush  attr = 0;          /* ascii/binary flag */
  47.     ush  deflate_flags = 0; /* pkzip -es, -en or -ex equivalent */
  48.  
  49.     ifd = in;
  50.     ofd = out;
  51.     outcnt = 0;
  52.  
  53.     /* Write the header to the gzip file. See algorithm.doc for the format */
  54.  
  55.     method = DEFLATED;
  56.     put_byte(GZIP_MAGIC[0]); /* magic header */
  57.     put_byte(GZIP_MAGIC[1]);
  58.     put_byte(DEFLATED);      /* compression method */
  59.  
  60.     if (save_orig_name) {
  61.     flags |= ORIG_NAME;
  62.     }
  63.     put_byte(flags);         /* general flags */
  64.     put_long(time_stamp);
  65.  
  66.     /* Write deflated file to zip file */
  67.     crc = updcrc(0, 0);
  68.  
  69.     bi_init(out);
  70.     ct_init(&attr, &method);
  71.     lm_init(level, &deflate_flags);
  72.  
  73.     put_byte((uch)deflate_flags); /* extra flags */
  74.     put_byte(OS_CODE);            /* OS identifier */
  75.  
  76.     if (save_orig_name) {
  77.     char *p = basename(ifname); /* Don't save the directory part. */
  78.     do {
  79.         put_char(*p);
  80.     } while (*p++);
  81.     }
  82.     header_bytes = (long)outcnt;
  83.  
  84.     (void)deflate();
  85.  
  86. #if !defined(NO_SIZE_CHECK) && !defined(RECORD_IO)
  87.   /* Check input size (but not in VMS -- variable record lengths mess it up)
  88.    * and not on MSDOS -- diet in TSR mode reports an incorrect file size)
  89.    */
  90.     if (ifile_size != -1L && isize != (ulg)ifile_size) {
  91.     Trace((stderr, " actual=%ld, read=%ld ", ifile_size, isize));
  92.     fprintf(stderr, "%s: %s: file size changed while zipping\n",
  93.         progname, ifname);
  94.     }
  95. #endif
  96.  
  97.     /* Write the crc and uncompressed size */
  98.     put_long(crc);
  99.     put_long(isize);
  100.     header_bytes += 2*sizeof(long);
  101.  
  102.     flush_outbuf();
  103.     return OK;
  104. }
  105.  
  106.  
  107. /* ===========================================================================
  108.  * Read a new buffer from the current input file, perform end-of-line
  109.  * translation, and update the crc and input file size.
  110.  * IN assertion: size >= 2 (for end-of-line translation)
  111.  */
  112. int file_read(buf, size)
  113.     char *buf;
  114.     unsigned size;
  115. {
  116.     unsigned len;
  117.  
  118.     Assert(insize == 0, "inbuf not empty");
  119.  
  120.     len = read(ifd, buf, size);
  121. #ifdef _SPD_PROG_
  122.     SPDNow=(unsigned long int)tell(ifd);
  123.     if(SPDSystemTask()) raise(SIGINT);
  124. #endif 
  125.  
  126.     if (len == (unsigned)(-1) || len == 0) return (int)len;
  127.  
  128.     crc = updcrc((uch*)buf, len);
  129.     isize += (ulg)len;
  130.     return (int)len;
  131. }
  132.